trigger InsertHospitalChangeHistory on Hospital__c (before update,after update) {
    if(trigger.new.size()>1 && system.trigger.isafter){
        list<id> hidl=new list<id>();
        list<id> rejectl=new list<id>();
        for(hospital__c h:trigger.new){
            if(Trigger.oldMap.get(h.id).approvalstatus__c == '审批通过但未生效'&& h.approvalstatus__c=='审批通过'){
                hidl.add(h.id);
            }
            else if(Trigger.oldMap.get(h.Id).approvalstatus__c != h.approvalstatus__c && h.approvalstatus__c == '审批驳回'){
                rejectl.add(h.id);
            }
        }
        if(hidl.size()>0){
            InsertHospitalChangeHistoryClass.RunMethod(hidl);
        }
        if(rejectl.size()>0){
            InsertHospitalChangeHistoryClass.RejectMethod(rejectl);
        }
    }
    else if(trigger.new.size()==1 && system.trigger.isbefore){
             //=======================================================================================
             
                HospitalChangeHistory__c HCH = null;
                list<hcp__c> updatehcp=new list<hcp__c>();
                List<HCP__c> associatedHCP = new List<HCP__c>(); 
                
                 if(Trigger.oldMap.get(trigger.new[0].id).approvalstatus__c == '审批通过但未生效'&&trigger.new[0].approvalstatus__c=='审批通过'){
                 hospital__c h=new hospital__c();
                hospital_approve__c ha=[select id,Name, approvalstatus__c,owner__c, class__c, approvaltype__c, Active__c, 
                                            classchangeto__c, ownerchangeto__c, namechangeto__c, comments__c from Hospital_approve__c 
                                            where hospital__c=: trigger.new[0].id];
                
                   hospital__c hos=trigger.new[0];
                    updatehcp.clear();
                        if(ha.approvaltype__c == '新增医院'){
                            HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                           Name = ha.approvaltype__c,
                                                           Old__c='',
                                                           New__c='',
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                        
                            hos.Active__c = true;
                            

                        }else if(ha.approvaltype__c==''||ha.approvaltype__c==null){
                              HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                           Name = '重新激活医院',
                                                           Old__c='',
                                                           New__c='',
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                            hos.Active__c = true;
                        }
                        
                        
                        else if(ha.approvaltype__c == '删除医院'){
                            HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                           Name = ha.approvaltype__c,
                                                           Old__c='',
                                                           New__c='',
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                            hos.Active__c = false;
                            associatedHCP = [select Active__c from HCP__c where Hospital__c =: hos.Id];
                            list<HCPChangehistory__c> inshcp=new list<HCPChangehistory__c>();
                            for (HCP__c hcp : associatedHCP){
                   //             hcp.Active__c = false;
                   //             updatehcp.add(hcp);
                              HCPChangeHistory__c HC = new HCPChangeHistory__c (HCP__c=hcp.Id, 
                                                                            Name = ha.approvaltype__c,
                                                                            Old__c='',
                                                                            New__c='',
                                                                            comments__c=hos.comments__c,
                                                                            effectivedate__c=hos.effectivedate__c);
                             inshcp.add(hc);
                            }
                  //          update updatehcp;
                            
                            insert inshcp;
                            list<id> rtidl=new list<id>();
                            list<recordtype> rt=[select id from recordtype where (name=:'科内会' and sobjecttype=:'hcpeducation__c') or (name=:'妈妈班' and sobjecttype=:'consumereducation__c') ];
                            for(recordtype rtid:rt){
                                rtidl.add(rtid.id);
                            }
                            list<hcpeducation__c> hcpedul=[select id from hcpeducation__c where (Hospital__c =:hos.id or OrganigerHospital__c =:hos.id) and date__c>=:date.today() and recordtypeid in:rtidl ];
                            if(hcpedul.size()>0){
                                for(hcpeducation__c hcpedu:hcpedul){
                                    hcpedu.deletemark__c=true;
                                }
                                update hcpedul;
                                delete hcpedul;
                            }
                            list<ConsumerEducation__c> csedul=[select id from ConsumerEducation__c where (Hospital__c =:hos.id or OrganigerHospital__c =:hos.id) and date__c>=:date.today() and recordtypeid in:rtidl ];
                            if(csedul.size()>0){
                                for(consumereducation__c csedu:csedul){
                                    csedu.deletemark__c=true;
                                }
                                update csedul;
                                delete csedul;
                            }
                            /*
                             *  Delete the Hospital budget.
                             *  Modified by Shuai Cao
                             */
                             
                            list<HospitalMonthlyAllocation__c> HMA =[select id from HospitalMonthlyAllocation__c where Hospital__c =:hos.id and ((Year__c =:String.valueof(date.today().Year()) and Month__c >: String.valueof(date.today().Month())) or (Year__c >:String.valueof(date.today().Year())))];
                            if(HMA.size()>0)
                            {
                                Delete HMA;
                            }
                            
                        }
                        else if(ha.approvaltype__c == '医院重命名'){
                            HCH = new HospitalChangeHistory__c (
                                                            Hospital__c=hos.Id, 
                                                           Name = ha.approvaltype__c,
                                                           Old__c=hos.Name,
                                                           New__c=ha.namechangeto__c,
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                            hos.Name = ha.namechangeto__c;
                        }
                        else if(ha.approvaltype__c == '改变医院级别'){
                            HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                           Name = ha.approvaltype__c,
                                                           Old__c=hos.class__c,
                                                           New__c=ha.classchangeto__c,
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                            hos.class__c = ha.classchangeto__c;
                        }
                        else if(ha.approvaltype__c == '改变MSR'){
                            user userold=[select lastname,firstname,id from user where id=:hos.ownerid];
                            user usernew=[select lastname,firstname,id from user where id=:ha.ownerchangeto__c];
                            HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                           Name = ha.approvaltype__c,
                                                           Old__c=userold.lastname+userold.firstname,
                                                           New__c=usernew.lastname+usernew.firstname,
                                                           comments__c=ha.comments__c,
                                                           effectivedate__c=hos.effectivedate__c);
                            hos.OwnerId = ha.ownerchangeto__c;
                            //改变MSR后删除相应CALLPLAN；
                            List<hcp__c> hcpl =new list<hcp__c>();
                            list<hcpeducation__c> hcpedul=new list<hcpeducation__c>();
                            list<consumereducation__c> csedul=new list<consumereducation__c>();
                            list<recordtype> rt=new list<recordtype>();

                            hcpl=[select id from hcp__c where hospital__c=:trigger.new[0].id];
                            if(hcpl.size()>0){
                                list<callplan__c> cpl=new list<callplan__c>();
                                cpl=[select id from callplan__c where CallPlanDate__c>=:date.today() and HCP__c in:hcpl];
                                for(callplan__c cp:cpl){
                                   cp.delete_mark__c=true;
                                }
                                if(cpl.size()>0){
                                    update cpl;
                                    delete cpl;
                                }
                            }
                            list<hcpeducation__c> hcpedudel=new list<hcpeducation__c>();
                            list<id> rtidl=new list<id>();
                            rt=[select id from recordtype where (name=:'科内会' and sobjecttype=:'hcpeducation__c') or (name=:'妈妈班' and sobjecttype=:'consumereducation__c') ];
                            for(recordtype rtid:rt){
                                rtidl.add(rtid.id);
                            }
                            hcpedul=[select id from hcpeducation__c where Hospital__c=:trigger.new[0].id and recordtypeid in:rtidl and date__c>=:date.today()];
                            if(hcpedul.size()>0){
                                for(hcpeducation__c hcp:hcpedul){
                                    hcp.deletemark__c=true;
                                    hcpedudel.add(hcp);
                                }
                                update hcpedudel;
                                delete hcpedudel;
                            }
                            
                           list<consumereducation__c> csedudel=new list<consumereducation__c>();
                  //         rt=[select id from recordtype where name=:'妈妈班' and sobjecttype=:'consumereducation__c'];
                           csedul=[select id from consumereducation__c where hospital__c=:trigger.new[0].id and recordtypeid in:rtidl and date__c>=:date.today()];
                           if(csedul.size()>0){
                               for(consumereducation__c cs:csedul){
                                   cs.deletemark__c=true;
                                   csedudel.add(cs);
                               }
                           update csedudel;
                           delete csedudel;
                           }   
                           
                            /*
                            *  Change the Hospital Material
                            *  Modified by Shuai Cao
                            */
                            List<HospitalMaterial__c> NeedChangeHM = [Select ownerid, id From HospitalMaterial__c Where ownerid =: userold.id and EndDate__c >=: date.today() ];
                            for(HospitalMaterial__c hm : NeedChangeHM)
                            {
                                hm.ownerid = usernew.id;
                            }
                            update NeedChangeHM;
                        }
             //         system.assert(hch==null,hch.name);
                        insert HCH;
                        
                        hos.approvaltype__c = null;
                        hos.effectivedate__c=null;
                        hos.approvalstatus__c=null;
                        hos.comments__c=null;
                        ha.approvaltype__c = null;
                        ha.classchangeto__c = null;
                        ha.namechangeto__c = null;
                        ha.ownerchangeto__c = null;
                        ha.effectivedate__c = null;
                        ha.comments__c = null;
                        ha.approvalstatus__c=null;
                        
                        
                        update ha;
                    }
                    else if(Trigger.oldMap.get(trigger.new[0].Id).approvalstatus__c != trigger.new[0].approvalstatus__c && 
                       trigger.new[0].approvalstatus__c == '审批驳回'){
                       hospital__c h=trigger.new[0];
                       HCH = new HospitalChangeHistory__c (Hospital__c=h.Id, 
                                               Name = '审批拒绝:'+h.approvaltype__c,
                                               comments__c=h.comments__c,
                                               effectivedate__c=date.today());

                        h.effectivedate__c = null;
                        h.comments__c=null;
                        h.approvalstatus__c=null;
                        if(h.approvaltype__c != '新增医院'){
                            h.approvaltype__c=null;
                        }
                        insert hch;
                       
                        list<hospital_approve__c> hal=[select id,classchangeto__c,namechangeto__c,ownerchangeto__c,effectivedate__c,comments__c from hospital_approve__c where hospital__c =: trigger.new[0].id ];
                        if(hal.size()>0){
                            for(hospital_approve__c ha:hal){
                                ha.classchangeto__c = null;
                                ha.namechangeto__c = null;
                                ha.ownerchangeto__c = null;
                                ha.effectivedate__c = null;
                                ha.comments__c = null;
                            }
                            update hal;
                        }
                    }       
                
                
                
             
             //=======================================================================================
         
    }    

}